package com.hnx.admin.aurora.core.func;

import com.hnx.admin.aurora.annotation.UpdateInfo;
import com.hnx.admin.aurora.core.SafeSqlBuilder;
import com.hnx.admin.aurora.core.SqlWithParams;
import com.hnx.admin.aurora.core.api.BuildSqlStrategy;
import com.hnx.admin.aurora.security.utils.SecurityUtil;
import com.hnx.admin.aurora.system.domain.vo.user.entity.UserDetailVO;
import com.hnx.admin.aurora.system.domain.vo.user.entity.UserVO;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/*
创建人： 倔强的头发
创建时间：2025/7/3
功能描述:

*/

public class BuildUpdateSql implements BuildSqlStrategy {
    @Override
    public SqlWithParams process(Method method, Object baseForm) {
        UpdateInfo annotation = method.getAnnotation(UpdateInfo.class);
        if (Objects.nonNull(annotation)) {
            UserDetailVO userDetailVO = (UserDetailVO) SecurityUtil.getPrincipal();
            if (Objects.nonNull(userDetailVO)) {
                Object id = getId(baseForm);
                if (Objects.nonNull(id)) {
                    UserVO userVO = userDetailVO.getUserVO();
                    Map<String, Object> params = new HashMap<>();
                    params.put(USER_ID, userVO.getUserId());
                    params.put(ID, id);
                    SafeSqlBuilder builder = new SafeSqlBuilder()
                            .update(TABLE)
                            .set("update_by", params.get(USER_ID))
                            .setExpression("update_time", "sysdate()")
                            .where("id = ?", params.get(ID));
                    return new SqlWithParams(builder.getSql(), builder.getParams());
                }
            }
        }
        return null;
    }
}
